package com.ziq.zcamp.community

import android.content.Context
import com.blankj.utilcode.util.ScreenUtils
import com.donkingliang.groupedadapter.adapter.GroupedRecyclerViewAdapter
import com.donkingliang.groupedadapter.holder.BaseViewHolder
import com.ziq.zcamp.R
import com.ziq.zcamp.bean.life.CampLifeEntranceGroup
import com.ziq.zcamp.bean.place.DetailGroup
import com.ziq.zcamp.bean.post.PostSubject
import com.ziq.zcamp.bean.post.SubjectGroup
import com.ziq.zcamp.manager.GlobalContextManager

class SubjectAdapter(context: Context) : GroupedRecyclerViewAdapter(context){

    companion object {
        const val TYPE_SUBJECT = 1
        const val TYPE_MORE = 2
        const val TYPE_ENTRANCE = CampLifeEntranceGroup.TYPE_CAMP_LIFE
    }

    interface SubjectAdapterAction{
        fun itemClick(item: PostSubject)
        fun moreClick()
    }

    var subjectAdapterAction: SubjectAdapterAction? = null

    var groupList: MutableList<DetailGroup> = mutableListOf()
    var subjectGroup = SubjectGroup(this)
    var campLifeGroup = CampLifeEntranceGroup(this)

    var itemWidth = (ScreenUtils.getScreenWidth() - GlobalContextManager.getResources().getDimensionPixelSize(
        R.dimen.dimens_dip_24)) / 2

    init {
        groupList.add(campLifeGroup)
        groupList.add(subjectGroup)
    }

    fun setData(list: List<PostSubject>) {
        //3.21.00 不显示露营人生
//        campLifeGroup.list = mutableListOf(CampLifeManager.myCampData.value?.camping_count?:0)
        subjectGroup.setData(list)
        notifyDataSetChanged()
    }

    fun setCampData(num : Int){
        //3.21.00 不显示露营人生
//        campLifeGroup.list = mutableListOf(num)
//        notifyDataSetChanged()
    }

    override fun getGroupCount(): Int {
        return groupList.size
    }

    override fun getChildrenCount(groupPosition: Int): Int {
        return groupList.get(groupPosition).getChildrenCount()
    }

    override fun hasHeader(groupPosition: Int): Boolean {
        return groupList[groupPosition].hasHeader(groupPosition)
    }

    override fun hasFooter(groupPosition: Int): Boolean {
        return groupList[groupPosition].hasFooter(groupPosition)
    }

    override fun getHeaderViewType(groupPosition: Int): Int {
        return groupList[groupPosition].getHeaderViewType(groupPosition)
    }

    override fun getChildViewType(groupPosition: Int, childPosition: Int): Int {
        return groupList[groupPosition].getChildViewType(groupPosition, childPosition)
    }

    override fun getHeaderLayout(viewType: Int): Int {
        return 0
    }

    override fun getFooterLayout(viewType: Int): Int {
        return 0
    }

    override fun getChildLayout(viewType: Int): Int {
        return when (viewType) {
            TYPE_SUBJECT -> {
                R.layout.item_home_subject
            }
            TYPE_MORE -> {
                R.layout.item_home_subject_more
            }
            TYPE_ENTRANCE -> {
                R.layout.item_camp_life_entrance
            }
            else -> {
                0
            }
        }
    }

    override fun onBindHeaderViewHolder(holder: BaseViewHolder?, groupPosition: Int) {
        groupList[groupPosition].onBindHeaderViewHolder(holder, groupPosition)
    }

    override fun onBindFooterViewHolder(holder: BaseViewHolder?, groupPosition: Int) {
        groupList[groupPosition].onBindFooterViewHolder(holder, groupPosition)

    }

    override fun onBindChildViewHolder(
        holder: BaseViewHolder?,
        groupPosition: Int,
        childPosition: Int
    ) {
        groupList[groupPosition].onBindChildViewHolder( holder, groupPosition, childPosition)
    }
}
